package per.mnxc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import per.mnxc.common.R;
import per.mnxc.pojo.Column;
import per.mnxc.service.IColumnService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mnxc
 * @since 2022-11-26
 */
@Api(tags = {"类型管理相关接口"})
@RestController
@RequestMapping("/column")
public class ColumnController {

    @Autowired
    IColumnService columnService;

    /**
     * @description: 类型添加接口
     * @author: mnxc
     * @date: 2022/11/27 20:48
     * @param: [column]
     * @return: per.mnxc.common.R<java.lang.String>
     **/
    @ApiOperation("类型添加接口")
    @ApiImplicitParam(name = "column",value = "类型信息",required = true)
    @PostMapping
    public R<String> save(@RequestBody Column column){
        //1.判断类型名称是否重名
        LambdaQueryWrapper<Column> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Column::getTypename,column.getTypename());
        Column one = columnService.getOne(lqw);
        if(one!=null){
            return R.error("类型名称重复，添加失败");
        }
        columnService.save(column);
        return R.success("添加成功");
    }

    /**
     * @description: 批量逻辑叶子节点类型
     * @author: mnxc
     * @date: 2022/11/27 21:02
     * @param: [ids]
     * @return: per.mnxc.common.R<java.lang.String>
     **/
    @ApiOperation("批量逻辑叶子节点类型")
    @DeleteMapping("/{ids}")
    public R<String> removeLeavesByIds(@PathVariable List<Long> ids){
        return columnService.removeLeavesByIds(ids);
    }

    /**
     * @description: 修改类型信息
     * @author: mnxc
     * @date: 2022/11/27 21:05
     * @param: [column]
     * @return: per.mnxc.common.R<java.lang.String>
     **/
    @ApiOperation("修改类型信息")
    @PutMapping
    public R<String> update(@RequestBody Column column){
        columnService.updateById(column);
        return R.success("修改成功");
    }

    /**
     * @description: 类型分页查询接口
     * @author: mnxc
     * @date: 2022/11/27 21:07
     * @param: [page, pageSize, name]
     * @return: per.mnxc.common.R<com.baomidou.mybatisplus.core.metadata.IPage<per.mnxc.pojo.Column>>
     **/
    @ApiOperation("类型分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码，从1开始",paramType = "query",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示记录数，从1开始",paramType = "query",required = true),
            @ApiImplicitParam(name = "name",value = "查询条件-类型名称",paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<Column>> page(int page,int pageSize,String name){
        return columnService.page(page,pageSize,name);
    }

    // @GetMapping
    public R<List<Column>> getAll(){
        return columnService.getAll();
    }
}

